/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Edit.java
 *
 * Created on 30.11.2011., 10.06.58
 */
package tok.predmeta;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.awt.Component;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author mega
 */
public class Edit extends javax.swing.JFrame {

    private Component frame;
   // private String trazena_sifra;
    private Component frame1;
    private int IDreda;

    /** Creates new form Edit */
    public Edit() {
        initComponents();

        jTextField2.setVisible(false);
        jTextField3.setVisible(false);
        jTextArea1.setVisible(false);
        jTextField4.setVisible(false);
       
        jTextField6.setVisible(false);
        jTextField7.setVisible(false);
        jComboBox1.setVisible(false);
        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        jLabel4.setVisible(false);
        jLabel5.setVisible(false);
        jLabel6.setVisible(false);
        
        jLabel8.setVisible(false);
        jLabel9.setVisible(false);
       
        jLabel12.setVisible(false);
        snimi.setVisible(false);
        jTextField1.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                warn();
            }

            public void warn() {
                String driverName = "com.mysql.jdbc.Driver"; // MySQL  JDBC driver
                try {
                    Class.forName(driverName);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
                }

// Create a connection to the database
                String serverName = "192.168.88.10";
                String mydatabase = "tok_predmeta";
                String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; // a JDBC url
                String username = "tokpred";
                String password = "Lx3sAQdnYJFTxBhs";
                try {
                    Connection connection = (Connection) DriverManager.getConnection(url, username, password);
                    Statement s = (Statement) connection.createStatement();
                    s.executeQuery("SELECT *  FROM predmeti where sifra= " + jTextField1.getText() + "");//+a.getText(0, a.getLength())+
                    Statement g = (Statement) connection.createStatement();
                    g.executeQuery("SELECT *  FROM state where sifra= " + jTextField1.getText() + "");
                    ResultSet rs = s.getResultSet();
                    ResultSet rs2 = g.getResultSet();

                    int count = 0;

                    while (rs.next()) {
                        int idVal = rs.getInt("id");
                        String sifra = rs.getString("sifra");
                        if (sifra.equals(jTextField1.getText())) {
                            String korisnicka_sifra = rs.getString("korisnicka_sifra");
                            String ime = rs.getString("ime");
                            String prezime = rs.getString("prezime");
                            String adresa = rs.getString("adresa");
                            String jmbg = rs.getString("jmbg");
                            String tip = rs.getString("tip");
                            String datum = rs.getString("datum");

                            while (rs2.next()) {
                               // String stanje_predmeta = rs2.getString("stanje_predmeta");
                               
                                IDreda = rs2.getInt("id");
                                jTextField6.setVisible(true);
                                jTextField6.setText(korisnicka_sifra);
                                jLabel2.setVisible(true);

                               




                            }

                            ++count;



                            jTextField2.setVisible(true);
                            jTextField2.setText(ime);

                            jTextField3.setVisible(true);
                            jTextField3.setText(prezime);

                            jTextArea1.setVisible(true);
                            jTextArea1.setText(adresa);

                            jTextField4.setVisible(true);
                            jTextField4.setText(jmbg);
                             jTextField7.setVisible(true);
                            jComboBox1.setVisible(true);

                            jComboBox1.setSelectedItem(tip);



                            jLabel2.setVisible(true);
                            jLabel3.setVisible(true);
                            jLabel4.setVisible(true);
                            jLabel5.setVisible(true);
                            jLabel6.setVisible(true);
                           
                            jLabel8.setVisible(true);
                            jLabel9.setVisible(true);
                             jLabel12.setVisible(true);
                            snimi.setVisible(true);
                            odustani.setVisible(true);
                            snimi.setVisible(true);



                        } else {
                            JOptionPane.showMessageDialog(frame1,
                                    "Предмет није пронађен !",
                                    "Први основни суд у Београду",
                                    JOptionPane.WARNING_MESSAGE);
                        }


                    }


                    rs.close();
                    s.close();


                } catch (SQLException ex) {
                    Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);

                    if (ex.getMessage().length() > 1) {
                        JOptionPane.showMessageDialog(frame1,
                                "Предмет није пронађен !",
                                "Први основни суд у Београду",
                                JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });

        jTextField7.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                warn();
            }

            public void warn() {
                if (jTextField7.getText().equals("01")) {
                     if (jTextField1.getText().equals("") || jTextField2.getText().equals("") || jTextField3.getText().equals("") || jTextArea1.getText().equals("")
                || jTextField4.getText().equals("") ||  jTextField6.getText().equals("")) {
            JOptionPane.showMessageDialog(frame,
                    "Молимо попуните сва поља !",
                    "Први основни суд у Београду",
                    JOptionPane.WARNING_MESSAGE);

        } else {
            String serverName = "192.168.88.10";
            String mydatabase = "tok_predmeta";
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; //?useUnicode=true&amp;characterEncoding=Cp1251"; // a JDBC url
            String username = "tokpred";
            String password = "Lx3sAQdnYJFTxBhs";
            String DATE_FORMAT = "dd/MM/yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            Calendar c1 = Calendar.getInstance(); // today
            String Datum = sdf.format(c1.getTime()).toString();
            try {
                java.sql.Connection connection = DriverManager.getConnection(url, username, password);
                java.sql.PreparedStatement i = connection.prepareStatement("UPDATE predmeti set ime=? , prezime=? , adresa = ? , jmbg = ?, tip = ? , datum = ? , korisnik = ?,korisnicka_sifra = ? where sifra = " + jTextField1.getText() + "");


                String stanje = jTextField4.getText();


                i.setString(1, jTextField2.getText());
                i.setString(2, jTextField3.getText());
                i.setString(3, jTextArea1.getText());
                i.setString(4, jTextField4.getText());
                i.setObject(5, jComboBox1.getSelectedItem());
                i.setString(6, Datum);
                i.setString(7, Login.user);
                i.setString(8, jTextField6.getText());


                i.executeUpdate();

                java.sql.PreparedStatement e = connection.prepareStatement("UPDATE state set korisnicka_sifra = ?  ,date = ?,korisnik = ? where id=" + IDreda + "");

                e.setString(1, jTextField6.getText());
               
                e.setString(2, Datum);
                e.setString(3, Login.user);

                e.executeUpdate();
                dispose();
                Frame1 P1 = new Frame1();
                P1.setVisible(true);

                i.close();
            } catch (SQLException ex) {
                Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
               
            }
        }
                } else if (jTextField7.getText().equals("02")) {
                    Frame1 f = new Frame1();
                    dispose();
                    f.setVisible(true);


                } else {
                }
            }
        });

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        odustani = new javax.swing.JButton();
        snimi = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Први основни суд у Београду - Ток обраде захтева");

        jLabel1.setText("Шифра предмета");

        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextField1FocusLost(evt);
            }
        });

        jLabel2.setText("Име");

        jLabel3.setText("Презиме");

        jLabel4.setText("Адреса");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(3);
        jTextArea1.setBorder(null);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel5.setText("ЈМБГ");

        jLabel6.setText("Тип предмета");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Притужба", "Захтев за информацију од јавног значаја", "Захтев за изузеће", "Представка" }));

        jLabel8.setForeground(new java.awt.Color(255, 0, 51));
        jLabel8.setText("ПАЖЊА ! У случају да треба да промените шифру предмета \nобратите се програмеру");

        jLabel9.setText("Корисничка шифра");

        odustani.setText("Одустани");
        odustani.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                odustaniActionPerformed(evt);
            }
        });

        snimi.setText("Сними");
        snimi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                snimiActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel11.setText("ИСПРАВКА ГРЕШКЕ");

        jLabel12.setText("Сними / Одустани");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 692, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, 409, Short.MAX_VALUE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(79, 79, 79))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(291, 291, 291))
                            .addComponent(jLabel12)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(odustani)
                                .addGap(189, 189, 189)
                                .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField4)
                                .addComponent(jLabel5)
                                .addComponent(jLabel3)
                                .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                                .addComponent(jTextField6)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(snimi)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(114, 114, 114)
                                .addComponent(jLabel11))
                            .addComponent(jLabel9))
                        .addGap(113, 113, 113))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel9))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addComponent(jLabel12)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(odustani)
                    .addComponent(snimi)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void odustaniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odustaniActionPerformed
        Frame1 F = new Frame1();
        this.dispose();
        F.setVisible(true);
    }//GEN-LAST:event_odustaniActionPerformed

    private void jTextField1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusLost
    }//GEN-LAST:event_jTextField1FocusLost

    private void snimiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_snimiActionPerformed
        if (jTextField1.getText().equals("") || jTextField2.getText().equals("") || jTextField3.getText().equals("") || jTextArea1.getText().equals("")
                || jTextField4.getText().equals("") ||  jTextField6.getText().equals("")) {
            JOptionPane.showMessageDialog(frame,
                    "Молимо попуните сва поља !",
                    "Први основни суд у Београду",
                    JOptionPane.WARNING_MESSAGE);

        } else {
            String serverName = "192.168.88.10";
            String mydatabase = "tok_predmeta";
            String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; //?useUnicode=true&amp;characterEncoding=Cp1251"; // a JDBC url
            String username = "tokpred";
            String password = "Lx3sAQdnYJFTxBhs";
            String DATE_FORMAT = "dd/MM/yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            Calendar c1 = Calendar.getInstance(); // today
            String Datum = sdf.format(c1.getTime()).toString();
            try {
                java.sql.Connection connection = DriverManager.getConnection(url, username, password);
                java.sql.PreparedStatement i = connection.prepareStatement("UPDATE predmeti set ime=? , prezime=? , adresa = ? , jmbg = ?, tip = ? , datum = ? , korisnik = ?,korisnicka_sifra = ? where sifra = " + jTextField1.getText() + "");


                String stanje = jTextField4.getText();


                i.setString(1, jTextField2.getText());
                i.setString(2, jTextField3.getText());
                i.setString(3, jTextArea1.getText());
                i.setString(4, jTextField4.getText());
                i.setObject(5, jComboBox1.getSelectedItem());
                i.setString(6, Datum);
                i.setString(7, Login.user);
                i.setString(8, jTextField6.getText());


                i.executeUpdate();

                java.sql.PreparedStatement e = connection.prepareStatement("UPDATE state set korisnicka_sifra = ? ,date = ?,korisnik = ? where id=" + IDreda + "");

                e.setString(1, jTextField6.getText());
                
                e.setString(2, Datum);
                e.setString(3, Login.user);

                e.executeUpdate();
                this.dispose();
                Frame1 P1 = new Frame1();
                P1.setVisible(true);

                i.close();
            } catch (SQLException ex) {
                Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(frame,
                        "Грешка приликом уписа у базу података. Предмет са таквим бројем или сифра корисника већ постоји !",
                        "Први основни суд у Београду",
                        JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_snimiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Edit().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JButton odustani;
    private javax.swing.JButton snimi;
    // End of variables declaration//GEN-END:variables
}
